/**
 * 物品库
 */
Lib = cc.Sprite.extend({
	libArr: [],
	openFlag:false,
	doing:false,
	rm : 10,
	ctor:function(p){
		this._super("#lib_back.png");
		p.addChild(this,30);
		this.setTag(TAG_LIB);
		this.init();
	},
	init:function(){
		this.libArr = [];
		this.openFlag = false;
		this.setPosition(gg.width + this.width * 0.5,550);
		
		var cylinder=new LibButton(this,10,TAG_LIB_CYLINDER,"#cylinder.png",this.callback,this);
		cylinder.setPosition(cc.p(60, this.height*0.5));
		
		var bottle=new LibButton(this,10,TAG_LIB_BOTTLE,"#bottle.png",this.callback,this);
		bottle.right(cylinder,this.rm);
		
		var kettle=new LibButton(this,10,TAG_LIB_KETTLE,"#kettle.png",this.callback,this);
		kettle.right(bottle,this.rm);
		
		var beaker=new LibButton(this,10,TAG_LIB_BEAKER,"#beaker.png",this.callback,this);
		beaker.right(kettle, this.rm);
		
		var rod=new LibButton(this,10,TAG_LIB_ROD,"#rod.png",this.callback,this);
		rod.right(beaker, this.rm);
		
		var flask=new LibButton(this,10,TAG_LIB_FLASK,"#flask.png",this.callback,this);
		flask.right(rod, this.rm);
		
		var pipette=new LibButton(this,10,TAG_LIB_PIPETTE,"#pipette.png",this.callback,this);
		pipette.right(flask,this.rm);
		
		var K2CrO4=new LibButton(this,10,TAG_LIB_K,"#K2CrO4.png",this.callback,this);
		K2CrO4.right(pipette,this.rm);
		
		var pipette1=new LibButton(this,10,TAG_LIB_PIPETTE1,"#pipette1.png",this.callback,this);
		pipette1.right(K2CrO4,this.rm)
	
		var ironShelf=new LibButton(this,10,TAG_LIB_IRONSHELF,"#ironShelf.png",this.callback,this);
		ironShelf.right(pipette1,this.rm);
		
		var buret=new LibButton(this,10,TAG_LIB_BURET,"#Buret.png",this.callback,this);
		buret.right(ironShelf, this.rm)
		
		var bottle1=new LibButton(this,10,TAG_LIB_BOTTLE1,"#bottle2.png",this.callback,this);
		bottle1.right(buret,this.rm);
	},
	moveLib:function(tag,width){
		width = 75;
		var begin = false;
		for(var i in this.libArr){
			var libTag = this.libArr[i];
			if(tag == libTag){
				begin = true;
			}
			if(begin){
				var lib = this.getChildByTag(libTag);
				if(lib != null){
					lib.runAction(cc.moveBy(0.5,cc.p(-width, 0)));
				}
			}
		}
	},
	callback:function(p){
		var pos = this.getPosition(); 
		var action = gg.flow.flow.action;
		switch(p.getTag()){
		case TAG_LIB_BOTTLE:
			ll.run.loadBottle(pos);
			break;
		case TAG_LIB_CYLINDER:
			ll.run.loadCylinder(pos);
			break;
		case TAG_LIB_KETTLE:
			ll.run.loadKettle(pos);
			break;
		case TAG_LIB_BEAKER:
			ll.run.loadBeaker(pos);
			break;
		case TAG_LIB_FLASK:
			ll.run.loadFlask(pos);
			break;
		case TAG_LIB_ROD:
			ll.run.loadRod(pos);
			break;
		case TAG_LIB_PIPETTE:
			ll.run.loadPipette(pos);
			break;
		case TAG_LIB_K:
			ll.run.loadK2CrO4(pos);
			break;
		case TAG_LIB_BOTTLE1:
			ll.run.loadBottle1(pos);
			break;
		case TAG_LIB_IRONSHELF:
			ll.run.loadIronShelf(pos);
			break;
		case TAG_LIB_BURET:
			ll.run.loadBuret(pos);
			break;
		case TAG_LIB_PIPETTE1:
			ll.run.loadPipette1(pos);
			break;
		default:
			break;
		}
		if(action == ACTION_NONE){
			this.moveLib(p.getTag(), p.width * p.getScale());
			p.removeFromParent(true);
		}
	},
	isOpen:function(){
		return this.openFlag; 
	},
	open:function(){
		if(this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(-this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = true;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			 if(tag instanceof Array){
				 if(TAG_LIB_MIN < tag[1]){
					 // 显示箭头
					 gg.flow.location();
				 }
			 }
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	},
	close:function(){
		if(!this.openFlag || this.doing){
			return;
		}
		this.doing = true;
		var move = cc.moveBy(0.4, cc.p(this.width,0));
		var func = cc.callFunc(function(){
			this.openFlag = false;
			this.doing = false;
			var tag = gg.flow.flow.tag;
			if(tag instanceof Array){
				if(TAG_LIB_MIN < tag[1]){
					// 隐藏箭头
//					ll.tip.arr.out();
					ll.tip.arr.pos(ll.tool.getChildByTag(TAG_BUTTON_LIB));	
				}
			}
		}, this);
		var seq = cc.sequence(move,func);
		this.runAction(seq);
	}
});
TAG_LIB_MIN = 30000;
TAG_LIB_BOTTLE=30001;
TAG_LIB_CYLINDER=30002;
TAG_LIB_KETTLE=30003;
TAG_LIB_BEAKER=30004;
TAG_LIB_FLASK=30005;
TAG_LIB_ROD=30006;
TAG_LIB_PIPETTE=30007;
TAG_LIB_K=30008;
TAG_LIB_IRONSHELF=30009;
TAG_LIB_BOTTLE1=30010;
TAG_LIB_BURET=30011;
TAG_LIB_PIPETTE1=30012;
libRelArr = [
             {tag:TAG_LIB_MIN, name:""},
             	{tag:TAG_LIB_BOTTLE,name:"生理盐水"},
             	{tag:TAG_LIB_CYLINDER,name:"量筒"},
             	{tag:TAG_LIB_KETTLE,name:"蒸馏水瓶"},
             	{tag:TAG_LIB_BEAKER,name:"烧杯"},
             	{tag:TAG_LIB_FLASK,name:"锥形瓶"},
             	{tag:TAG_LIB_ROD,name:"玻璃棒"},
             	{tag:TAG_LIB_PIPETTE,name:"移液管"},
             	{tag:TAG_LIB_K,name:"K2CrO4试剂"},
             	{tag:TAG_LIB_IRONSHELF,name:"铁架台"},
             	{tag:TAG_LIB_BOTTLE1,name:"AgNo3溶液"},
             	{tag:TAG_LIB_BURET,name:"酸式滴定管"},
             	{tag:TAG_LIB_PIPETTE1,name:"移液管"}
             ];
